package com.youhome.business.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2017/10/25.
 */
@RestController
@RequestMapping(value = "/tag")
@Api(tags = "标签")
public class TagController {
    @Autowired
    private BTagService bTagService;

    @ApiOperation(value = "创建标签", notes = "创建标签", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(dataType = "String", name = "tag", value = "标签", required = true, paramType = "query")
    @PostMapping(value = "/create")
    public Object create(String tag,@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bTagService.createTag(principal,tag));
    }

    @ApiOperation(value = "顾问所有标签", notes = "顾问所有标签", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/list/all")
    public Object listAll(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bTagService.selectAllTag(principal));
    }

    @ApiOperation(value = "给客户打标签", notes = "给客户打标签", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "clientId", value = "客户ID", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Long", name = "tagId", value = "标签ID", required = true, paramType = "query")
    })
    @PostMapping(value = "/make")
    public Object make(Long clientId, Long tagId) throws CustomException {
        bTagService.makeTag(clientId, tagId);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "删除标签", notes = "删除标签", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "tagId", value = "标签ID", required = true, paramType = "query")
    })
    @DeleteMapping(value = "/delete")
    public Object delete(Long tagId) throws CustomException {
        bTagService.deleteByTagId(tagId);
        return new Result(ResultCode.SUCCESS);
    }


    @ApiOperation(value = "客户拥有的标签", notes = "客户拥有的标签", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "clientId", value = "客户ID", required = true, paramType = "query")
    })
    @GetMapping(value = "/selected")
    public Object selected(Long clientId) throws CustomException {
        return new Result(ResultCode.SUCCESS, bTagService.selectAllTag(clientId));
    }

}
